package com.mbigger.admin.service.impl;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mbigger.admin.beans.PageQuery;
import com.mbigger.admin.beans.PageResult;
import com.mbigger.admin.common.RequestHolder;
import com.mbigger.admin.dao.AclMapper;
import com.mbigger.admin.dao.RoleAclMapper;
import com.mbigger.admin.dao.RoleUserMapper;
import com.mbigger.admin.entity.Acl;
import com.mbigger.admin.exception.ParamException;
import com.mbigger.admin.param.AclParam;
import com.mbigger.admin.service.AclService;
import com.mbigger.admin.util.BeanValidator;
import com.mbigger.admin.util.IpUtil;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AclServiceImpl implements AclService{

	@Autowired
	private AclMapper aclMapper;
	@Autowired
	private RoleUserMapper roleUserMapper;
	@Autowired
	private RoleAclMapper roleAclMapper;

	public void save(AclParam param) {
		BeanValidator.check(param);
		if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
			throw new ParamException("当前权限模块下面存在相同名称的权限点");
		}
		Acl acl = Acl.builder().name(param.getName()).aclModuleId(param.getAclModuleId()).permission(param.getPermission())
				.type(param.getType()).status(param.getStatus()).seq(param.getSeq()).remark(param.getRemark()).build();
		acl.setOperator(RequestHolder.getCurrentUser().getUsername());
		acl.setOperateTime(new Date());
		acl.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
		aclMapper.insert(acl);
	}

	public void update(AclParam param) {
		BeanValidator.check(param);
		if (checkExist(param.getAclModuleId(), param.getName(), param.getId())) {
			throw new ParamException("当前权限模块下面存在相同名称的权限点");
		}
		Acl before = aclMapper.selectById(param.getId());
		Preconditions.checkNotNull(before, "待更新的权限点不存在");

		Acl after = Acl.builder().id(param.getId()).name(param.getName()).aclModuleId(param.getAclModuleId())
				.permission(param.getPermission()).type(param.getType()).status(param.getStatus()).seq(param.getSeq())
				.remark(param.getRemark()).build();
		after.setOperator(RequestHolder.getCurrentUser().getUsername());
		after.setOperateTime(new Date());
		after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));

		aclMapper.updateById(after);
	}
	
	public List<Acl> getAclListByUserId(int userId) {
		List<Integer> roleIdList = roleUserMapper.getRoleIdListByUserId(userId);
		List<Integer> aclIdLis = roleAclMapper.getAclIdListByRoleIdList(roleIdList);
		if (CollectionUtils.isEmpty(aclIdLis)) {
			return Lists.newArrayList();
		}
		return aclMapper.getByIdList(aclIdLis);
	}

	public boolean checkExist(int aclModuleId, String name, Integer id) {
		return aclMapper.countByNameAndAclModuleId(aclModuleId, name, id) > 0;
	}

	public PageResult<Acl> getPageByAclModuleId(int aclModuleId, PageQuery page) {
		BeanValidator.check(page);
		int count = aclMapper.countByAclModuleId(aclModuleId);
		if (count > 0) {
			List<Acl> aclList = aclMapper.getPageByAclModuleId(aclModuleId, page);
			return PageResult.<Acl>builder().data(aclList).total(count).build();
		}
		return PageResult.<Acl>builder().build();
	}
}
